package com.rhino.xchat.ui.activity

import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.KeyboardUtils
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.rhino.base.extension.appInfo
import com.rhino.base.extension.toast
import com.rhino.base.ui.activity.BaseActionBarActivity
import com.rhino.xchat.R
import com.rhino.xchat.bean.MainTabBean
import com.rhino.xchat.constant.RouteConstant
import com.rhino.xchat.databinding.ActivityMainBinding
import com.rhino.xchat.ui.fragment.MainTabHomeFragment
import com.rhino.xchat.ui.fragment.MainTabMessageFragment
import com.rhino.xchat.ui.fragment.MainTabMineFragment
import com.rhino.xchat.ui.fragment.MainTabSquareFragment
import com.therouter.router.Route


@Route(path = RouteConstant.MAIN_ACTIVITY)
class MainActivity : BaseActionBarActivity<ActivityMainBinding>(),
    BottomNavigationView.OnNavigationItemSelectedListener {

    private var lastClickBackTimestamp: Long = 0

    private var switchAble = true
    private var menuItem: MenuItem? = null
    private var mainTabBean: MainTabBean? = null
    private val tabs by lazy {
        mutableListOf<MainTabBean>().apply {
            add(
                MainTabBean(
                    R.id.main_tab_home,
                    "首页",
                    R.drawable.selector_tab_home,
                    MainTabHomeFragment()
                )
            )
            add(
                MainTabBean(
                    R.id.main_tab_square,
                    "广场",
                    R.drawable.selector_tab_square,
                    MainTabSquareFragment()
                )
            )
            add(
                MainTabBean(
                    R.id.main_tab_message,
                    "消息",
                    R.drawable.selector_tab_message,
                    MainTabMessageFragment()
                )
            )
            add(
                MainTabBean(
                    R.id.main_tab_mine,
                    "我的",
                    R.drawable.selector_tab_mine,
                    MainTabMineFragment()
                )
            )
        }
    }


    override fun initTitle(): String? {
        return null
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_main
    }

    override fun initView() {
        rootBinding.defaultActionBar.setBottomLineVisible(false)
        rootBinding.defaultActionBar.setBottomLineShadowVisible(false)
        dataBinding.onClickListener = this
        KeyboardUtils.hideSoftInput(thisActivity)
        initTab()
        // 请求通知权限
//        Permission.requestPermission(Permission.push)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun registerLiveData() {

    }

    override fun onSingleClick(view: View) {

    }

    override fun onBackPressed() {
        if (System.currentTimeMillis() - lastClickBackTimestamp > 2000) {
            getString(R.string.one_more_exit_app).toast()
            lastClickBackTimestamp = System.currentTimeMillis()
            if (appInfo.isDebug) {
                thisHandler.postDelayed({

                }, 2000)
            }
        } else {
            super.onBackPressed()
        }
    }

    private fun initTab() {
        val position = 0
        dataBinding.navigation.itemTextColor =
            getColorStateList(R.color.color_navigation_text)
        dataBinding.navigation.itemIconTintList = null
        dataBinding.navigation.setOnNavigationItemSelectedListener(this)
        dataBinding.viewPager.isUserInputEnabled = false
        dataBinding.viewPager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                if (menuItem != null) {
                    menuItem?.isChecked = false
                } else {
                    dataBinding.navigation.menu.getItem(0).isChecked = false
                }
                menuItem = dataBinding.navigation.menu.getItem(position)
                menuItem?.isChecked = true
            }
        })

        // 动态添加tab
        for (i in tabs.indices) {
            val mainTabBean = tabs[i]
            dataBinding.navigation.menu
                .add(0, mainTabBean.id, i, mainTabBean.title)
                .setIcon(mainTabBean.iconResId)
        }
        //遍历子View,重写长按点击事件
        val viewGroup = dataBinding.navigation.getChildAt(0) as ViewGroup
        for (i in tabs.indices) {
            val mainTabBean = tabs[i]
            viewGroup.getChildAt(i).findViewById<View>(mainTabBean.id)
                .setOnLongClickListener { true }
        }
        dataBinding.viewPager.offscreenPageLimit = tabs.size
        dataBinding.viewPager.adapter = object : FragmentStateAdapter(this) {
            override fun createFragment(position: Int): Fragment {
                return tabs[position].fragment
            }

            override fun getItemCount(): Int {
                return tabs.size
            }
        }
        selectedTab(position, false)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        if (switchAble) {
            switchAble = false
            thisHandler.postDelayed(Runnable { switchAble = true }, 200)
            var position = 0
            for (i in tabs.indices) {
                if (tabs[i].id == item.itemId) {
                    position = i
                }
            }
            selectedTab(position, item.isChecked)
        }
        return false
    }

    private fun selectedTab(position: Int, fromUser: Boolean) {
        if (mainTabBean?.id == R.id.main_tab_mine) {
//            accountViewModel.getUserInfo()
        }
        mainTabBean = tabs[position]
        dataBinding.viewPager.setCurrentItem(position, false)
    }
}